def get_db_uri(dbinfo):
    USER = dbinfo.get('USER', 'root')
    PASSWORD = dbinfo.get('PASSWORD', '00000000')
    HOST = dbinfo.get('HOST', '127.0.0.1')
    PORT = dbinfo.get('PORT', '3306')
    DATABASE = dbinfo.get('DATABASE', 'msbd')

    return 'mysql+pymysql://{}:{}@{}:{}/{}'.format(
        USER, PASSWORD, HOST, PORT, DATABASE
    )


# 配置基类
class Config:
    # 秘钥
    SECRET_KEY = '123456'
    # 数据库
    SQLALCHEMY_COMMIT_ON_TEARDOWN = True
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    # 缓存
    CACHE_TYPE = 'redis'
    CACHE_REDIS_HOST = '127.0.0.1'
    CACHE_REDIS_PORT = 6379
    CACHE_REDIS_DB = 1
    # Token有效期
    TOKEN_LIFETIME = 3600 * 24
    # 路由白名单
    URL_WHITE_LIST = {
        '/auth/': ['POST'],
        '/user/': ['POST']
    }


# 开发环境配置
class DevelopConfig(Config):
    DATABASE = {
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'DATABASE': 'msbd'
    }
    SQLALCHEMY_DATABASE_URI = get_db_uri(DATABASE)


# 测试环境配置
class TestingConfig(Config):
    SQLALCHEMY_DATABASE_URI = 'TESTING'


# 生产环境配置
class ProductConfig(Config):
    SQLALCHEMY_DATABASE_URI = 'PRODUCT'


# 配置字典
config = {
    'develop': DevelopConfig,
    'testing': TestingConfig,
    'product': ProductConfig,
    # 默认环境
    'default': DevelopConfig
}
